package com.example.springbook.Controller;

import com.example.springbook.Model.BookInfo;
import com.example.springbook.Model.PageRequest;
import com.example.springbook.Model.PageResult;
import com.example.springbook.Service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;
    @RequestMapping("/getList")
    public List<BookInfo> getList() {
        //获取数据
        //交给spring注入依赖

        List<BookInfo> books = bookService.getBookList();
        //处理⻚⾯展⽰
        return books;
    }

    @RequestMapping("/addBook")
    public String addBook(BookInfo bookInfo){
        if(!StringUtils.hasLength(bookInfo.getBookName())
           ||!StringUtils.hasLength(bookInfo.getAuthor())
                ||bookInfo.getCount() == null
                ||!StringUtils.hasLength(bookInfo.getPublish())
                ||bookInfo.getStatus() ==null
                ||bookInfo.getPrice()==null
        ){
            return "输入的参数不合法";
        }
        try {
            Integer result=bookService.insertBook(bookInfo);
            if(result > 0){
                return "";
            }
        }catch (Exception e){
            log.error("添加图书异常");
        }
        return "添加图书失败";
    }

    @RequestMapping("/getBookListByPage")
    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest){
        return bookService.getBookListByPage(pageRequest);
    }
}
